<?phpclass img 
{
	var $max_w = 720;
	var $max_h = 540;
	var $pos_x = "RIGHT"; // pos logo
	var $pos_y = "BOTTOM"; // pos logo
	var $name = ""; //new image name

	function AddLogo($image, $logo = NULL)
	{
		$this->im = $this->createImage($image);
		$this->im_width = imagesx($this->im);
		$this->im_height = imagesy($this->im);

		$this->new_image = $this->resizeImg();

		if($logo != NULL)
		{
			$this->createTheLogo($logo);				
		}

		if($this->name==""){
			$this->name = substr($image, 0 , strrpos($image, "."));
		}

		if($this->type == 'jpg')
			imagejpeg($this->new_image, $this->name.".jpg");
		else
			imagepng($this->new_image, $this->name.".jpg");
	}
	
	function createImage($image){
		$size_img = getimagesize($image);
		if($size_img[2]==2)  return imagecreatefromjpeg($image);
		else if($size_img[2]==1) return imagecreatefromgif($image);
		else if($size_img[2]==3) return imagecreatefrompng($image);
		else $this->error("Не известный формат изображения");
	}
	
	function createTheLogo($logo)
	{
		$this->logo = $this->createImage($logo);
		$this->logo_width = imagesx($this->logo);
		$this->logo_height = imagesy($this->logo);

		$wh = $this->calc_pos_wh();
		$this->calc_pos_w = $wh[0];
		$this->calc_pos_h = $wh[1];

		$this->wt_x = $this->calc_pos_x($this->pos_x);
		$this->wt_y = $this->calc_pos_y($this->pos_y);
		//imagecopy($this->new_image, $this->logo, $this->wt_x, $this->wt_y, 0, 0,
		//											$this->logo_width, $this->logo_height);
		imagecopyresampled($this->new_image, $this->logo, $this->wt_x, $this->wt_y, 0, 0,
								$this->calc_pos_w,$this->calc_pos_h,$this->logo_width, $this->logo_height);
	}
        
        
	function resizeImg()
	{

		if($this->im_width > $this->max_w && $this->im_height < $this->max_h)
		{
		
			$rapporto = $this->max_w / $this->im_width;
			
			$this->new_im_w = $this->im_width * $rapporto;
			$this->new_im_h = $this->im_height * $rapporto;
			
		}
		
		else if($this->im_width < $this->max_w && $this->im_height > $this->max_h)
		{
		
			$rapporto = $this->max_h / $this->im_height;
			
			$this->new_im_w = $this->im_width * $rapporto;
			$this->new_im_h = $this->im_height * $rapporto;
		
		}
		else if($this->im_width > $this->max_w && $this->im_height > $this->max_h)
		{
		
			$rapporto_1 = $this->max_w / $this->im_width;
			$rapporto_2 = $this->max_h / $this->im_height;
				if($rapporto_1 > $rapporto_2)
				{
					$rapporto = $rapporto_2;
				}
				else
				{
					$rapporto = $rapporto_1;
				}
					
					$this->new_im_w = $this->im_width * $rapporto;
					$this->new_im_h = $this->im_height * $rapporto;
		
		}
		
		else
		{
			$this->new_im_w = $this->im_width;
			$this->new_im_h = $this->im_height;
		}
		
		$this->new_image = imagecreatetruecolor($this->new_im_w, $this->new_im_h);
		imagecopyresampled($this->new_image, $this->im, 0, 0, 0, 0, $this->new_im_w,
										$this->new_im_h, $this->im_width, $this->im_height);
		
		return $this->new_image;

	}
		
	function calc_pos_x($position_x)
        {
		$x = 0;
		switch($position_x)
		{
			case 'LEFT':
			    $x = 0;
			    break;
			case 'CENTER':
			    $x = @$this->new_im_w / 2 - @$this->calc_pos_w / 2;
			    break;
			case 'RIGHT':
			    $x = @$this->new_im_w - (@$this->calc_pos_w+2);
			    break;
			default:
			    $x = 0;
		}
            return $x;
        
        }
        
        function calc_pos_y($position_y)
        {
		$y = 0;
		switch($position_y)
		{
			case 'TOP':
			    $y = 0;
			    break;
			case 'MIDDLE':
			    $y = @$this->new_im_h / 2 - @$this->calc_pos_h / 2;
			    break;
			case 'BOTTOM':
			    $y = @$this->new_im_h - (@$this->calc_pos_h+2);
			    break;
			default:
			    $y = 0;
		}
	return $y;
        
        }

	function calc_pos_wh()
        {
		if($this->new_im_h>$this->new_im_w){
			$h = $this->new_im_h*0.3+5;
			$w = $this->new_im_w*0.5+5;
		}else {
			$h = $this->new_im_h*0.5+5;
			$w = $this->new_im_w*0.3+5;
		}

		$ratio = $this->new_im_w/$this->new_im_h;
		$src_ratio=$this->logo_width/$this->logo_height;

		if($ratio<$src_ratio){
			$h = $w/$src_ratio;
		}else{
			$w = $h*$src_ratio;
		}
		return array($w,$h);
        }        
}
?>
